<?php
namespace Home\Controller;
use Think\Controller;

class JmxxcxController extends Controller {
	//综合信息查询页
	public function xx_query() {
		$this->display();
	}
	//人体画像页
	public function rentihuaxiang() {
	    $renid = I('get.renid')+0;  //加0让它变成数字类型的
        $renData = M('People')->find($renid);
        $this->assign('renData',$renData);  //人物的详细信息
        //dump($renData);
		$this->display();
	}
	//混合查询页,未完善
	public function hunhe_query() {
		$this->display();
	}
	//类百度查询页
	public function query_list() {	
		$user = session('adminUser');
		$this->assign('user',$user);
		if (IS_GET) {
			session('query_keywords','');
			if(session('query_keywords')!=$_GET["query_keywords"]){
				session('query_keywords',$_GET["query_keywords"]);
				// $m = $this->fetch();
			}
			// echo session('query_keywords');
			$query_keywords = session('query_keywords');
			$this->assign('query_keywords',$query_keywords);

			$Model = D("people");
			$moren = $Model->where("name like '%".$query_keywords."%' OR idcard='$query_keywords'")->select();	
			$count = $Model->where("name like '%".$query_keywords."%' OR idcard='$query_keywords'")->count();		
			if($count>0){
				$this->assign('ren',$moren);
			}
			$this->assign('count',$count);
		}else{
			$this->error('未收到传值');
		}

		//打印出标签
		$id = session('user_id');  //用户id
		$res = M('Admin')->field('ren_dianji as 人$ren_dianji,fang_dianji as 房$fang_dianji,che_dianji as 车$che_dianji,danwei_dianji as 单位$danwei_dianji')->find($id);
		$arr =[];
		$res_index = array_keys($res);
		for($i = 0;$i<count($res_index);$i++){
		    $arr[] = explode('$',$res_index[$i]);
		}
		$res1 = array_column($arr,'0');
		$res2 = array_column($arr,'1');
		$res_value = array_values($res);
		$newArr = [];
		for($i = 0;$i<count($res);$i++){
		    $newArr[$i][] = $res1[$i];
		    $newArr[$i][] = $res2[$i];
		    $newArr[$i][] = $res_value[$i];
		}
		$sort = array_column($newArr, '2');

		array_multisort($sort, SORT_DESC, $newArr);
		$this->assign('biaoqian',$newArr);


		$this->display();
	}
	//点人标签次数增加方法
	public function ren_ciadd() {
		// $user = session('adminUser');
		$Model = M('admin');
		$id = trim(I('post.id'));
		$Model->where("id=$id")->setInc('ren_dianji');
	}
	//点车标签次数增加方法
	public function che_ciadd() {
		// $Model = M('admin');
		// $data['id'] = trim(I('post.id'));
		// $UID = $_POST['id'];
		// $value = $Model->where("id = $UID")->find();
		// $value['che_dianji']++;
		// $data['che_dianji'] = $value['che_dianji'];
		// M("admin")->save($data);
		$Model = M('admin');
		$id = trim(I('post.id'));
		$Model->where("id=$id")->setInc('che_dianji');
	}
	//点房标签次数增加方法
	public function fang_ciadd() {
		$Model = M('admin');
		$id = trim(I('post.id'));
		$Model->where("id=$id")->setInc('fang_dianji');
	}
	//点单位标签次数增加方法
	public function danwei_ciadd() {
		$Model = M('admin');
		$id = trim(I('post.id'));
		$Model->where("id=$id")->setInc('danwei_dianji');
	}
	//点人标签查询方法
	public function ren_easyqry() {
		$glModel = D("people");
		$skeyword = $_POST["skeyword"];
 		$zn_ren = $glModel->where("name like '%".$skeyword."%' OR idcard='$skeyword' ")->select();
 		echo json_encode($zn_ren); 
	}
	//点车标签查询方法
	public function che_easyqry() {
		$glModel = D("JmxxcxView");
		$skeyword = $_POST["skeyword"];
 		$zn_ren = $glModel->where("che_number like '%".$skeyword."%' OR name like '%".$skeyword."%' ")->select();
 		echo json_encode($zn_ren); 
	}
	//点单位标签查询方法
	public function danwei_easyqry() {
		$glModel = D("dwcx");
		$skeyword = $_POST["skeyword"];
 		$zn_ren = $glModel->where("workname like '%".$skeyword."%' OR faren_name like '%".$skeyword."%'")->select();
 		echo json_encode($zn_ren); 
	}
	//个人详情页
	public function gr_xiangxi() {
		$glModel = D("people");
		$id = isset($_GET['id']) ? intval($_GET['id']) : "";
		$where = array();
		$where['id'] = array('eq',"$id");
 		$zn_che = $glModel->where($where)->select();
 		$this->assign('grxx',$zn_che);
		$this->display();
	}
	//多条件查询人方法
	public function ren_select(){
		$Model = D("JmxxcxView");
		$where = array();
		
		$name = $_POST["name"];
	    $guoji = $_POST["guoji"];
	    $date = $_POST["date"];
	    $idcard = $_POST["idcard"];
	    $minzu = $_POST["minzu"];
	    $sex = $_POST["sex"];
	    $education = $_POST["education"];
	    $hunyin = $_POST["hunyin"];
	    $zhongdian = $_POST["zhongdian"];

	    if ($name) {
	    	$where['name'] = array('like',"%$name%");
	    }
	    if ($guoji) {
	    	$where['guoji'] = array('like',"%$guoji%");
	    }
	    if ($date) {
	    	$where['date'] = array('eq',"$date");
	    }
	    if($idcard){
	    	$where['idcard'] = array('eq',"$idcard");
	    } 
	    if ($minzu) {
	    	$where['minzu'] = array('like',"%$minzu%");
	    }
	    if ($sex<>-1) {
	    	$where['sex'] = array('eq',"$sex");
	    }
	    if ($education) {
	    	$where['education'] = array('like',"%$education%");
	    }
	    if ($hunyin<>-1) {
	    	if($hunyin==1){
	    		$hunyin = "已婚";
	    	}else{
	    		$hunyin = "未婚";
	    	}
	    	$where['hunyin'] = array('eq',"$hunyin");
	    }
	    if ($zhongdian<>-1) {
	    	$where['is_zhongdian'] = array('eq',"$zhongdian");
	    }

	    $zn_rencount = $Model->where($where)->count(); 
	    $zn_ren = $Model->where($where)->select();

	    if ($zn_rencount > 0){   
	    	echo json_encode($zn_ren);       
	    }
	    else{
	    	echo json_encode(0);
	    }
	}
	//人的信息预览框
	public function ren_xxylk(){
		$glModel = D("JmxxcxView");
		
		$id = $_POST["id"];

		$where = array();
 		$where['id'] = array('eq',"$id");
 		$zn_che = $glModel->where($where)->find();
    
	    echo json_encode($zn_che);        
	}
	//多条件查询单位方法
	public function danwei_select(){
		$Model = D("dwcx");

		$where = array();
		
		$workname = $_POST["workname"];
	    $location = $_POST["location"];
	    $faren_name = $_POST["faren_name"];
	    $baowei_name = $_POST["baowei_name"];

	    if ($workname) {
	    	$where['workname'] = array('like',"%$workname%");
	    }
	    if ($faren_name) {
	    	$where['faren_name'] = array('like',"%$faren_name%");
	    }
	    if($baowei_name){
	    	$where['baowei_name'] = array('like',"%$baowei_name%");
	    } 
	    if ($location) {
	    	$where['location'] = array('like',"%$location%");
	    }
	    // $zn_danweicount = $Model->where($where)->getLastSql();;

	    $zn_danweicount = $Model->where($where)->count();  
	    $zn_danwei = $Model->where($where)->select();

	    if ($zn_danweicount > 0){   
	    	echo json_encode($zn_danwei);       
	    }
	    else{
	    	echo json_encode(0);
	    }
	}
	//单位信息预览框
	public function danwei_xxylk(){
		$glModel = D("dwcx");
		
		$id = $_POST["id"];

		$where = array();
 		$where['danwei_id'] = array('eq',"$id");
 		$zn_danwei = $glModel->where($where)->find();
    
	    echo json_encode($zn_danwei);        
	}
    //多条件查询房方法
    public function fang_select(){
        if(IS_POST){
            $fangjian = $_POST["fangjian"];
            $sql = "SELECT b.fang_id,b.fangchanzhenghao,c.`name`,c.idcard,c.biaoshi,b.huxing,b.menjin_status,b.is_use
                    FROM zn_people_house a
                    LEFT JOIN zn_house b ON a.fangid=b.fang_id
                    LEFT JOIN zn_people c ON a.renid=c.id
                    WHERE b.fang_id ='".$fangjian."'";
            $myModel = M();
            $fxx = $myModel->query($sql);
            echo json_encode($fxx);
        }else{
            echo json_encode(0);
        }
    }
    //房信息概览框
    public function fang_xxylk(){
        if(IS_POST){
            $fangjian = $_POST["fangid"];
            $sql = "SELECT b.fang_id,b.fangchanzhenghao,c.`name`,c.idcard,c.biaoshi,b.huxing,b.menjin_status,b.is_use,b.huxing_pic
                    FROM zn_people_house a
                    LEFT JOIN zn_house b ON a.fangid=b.fang_id
                    LEFT JOIN zn_people c ON a.renid=c.id
                    WHERE b.fang_id ='".$fangjian."'";
            $myModel = M();
            $fxxgl = $myModel->query($sql);
            echo json_encode($fxxgl);
        }else{
            echo json_encode(0);
        }
    } 
    //级联-街道查小区
    public function jiedao_cha(){
        if(IS_POST){
            $jiedao = $_POST["jiedao"];
            $sql = "SELECT DISTINCT a.`name`,a.id
                    FROM zn_xiaoqu a,zn_house b 
                    WHERE a.id = b.xiaoquid
                    AND b.diqu='".$jiedao."'";
            $myModel = M();
            $pri = $myModel->query($sql);
            echo json_encode($pri);
        }else{
            $this->error("出错了",U("Home/index/index"));
        }
    }
    //级联-小区查楼号
    public function xiaoqu_cha(){
        if(IS_POST){
            $jiedao = $_POST["jiedao"];
            $xiaoqu = $_POST["xiaoqu"];
            $sql = "SELECT DISTINCT louhao
                    FROM zn_house
                    WHERE diqu= '".$jiedao."' AND xiaoquid='".$xiaoqu."'";
            $myModel = M();
            $louhao = $myModel->query($sql);
            echo json_encode($louhao);
        }else{
            $this->error("出错了",U("Home/index/index"));
        }
    }
    //级联-楼号查单元
    public function louhao_cha(){
        if(IS_POST){
            $jiedao = $_POST["jiedao"];
            $xiaoqu = $_POST["xiaoqu"];
            $louhao = $_POST["louhao"];
            $sql = "SELECT DISTINCT a.`name`,a.id
                    FROM zn_danyuan a,zn_house b 
                    WHERE a.id = b.danyuanid
                    AND diqu= '".$jiedao."' AND xiaoquid='".$xiaoqu."' AND louhao='".$louhao."' ";
            $myModel = M();
            $danyuan = $myModel->query($sql);
            echo json_encode($danyuan);
        }else{
            $this->error("出错了",U("Home/index/index"));
        }
    }
    //级联-单元查房间号
    public function danyuan_cha(){
        if(IS_POST){
            $jiedao = $_POST["jiedao"];
            $xiaoqu = $_POST["xiaoqu"];
            $louhao = $_POST["louhao"];
            $danyuan = $_POST["danyuan"];
            $sql = "SELECT DISTINCT a.fang_id,a.fangjianhao
                    FROM zn_house a 
                    WHERE diqu= '".$jiedao."' AND xiaoquid='".$xiaoqu."' AND louhao='".$louhao."' AND danyuanid='".$danyuan."' ";
            $myModel = M();
            $danyuan = $myModel->query($sql);
            echo json_encode($danyuan);
        }else{
            $this->error("出错了",U("Home/index/index"));
        }
    }
	//多条件查车方法
	public function che_select(){
		// $zn_cheModel = D("zn_che");
		// $zn_peopleModel = D("zn_people");
		$Model = D("JmxxcxView");


		// 相同写法
		// $Model = D("JmxxcxView");
		// $Model->table('think_JmxxcxView JmxxcxView,think_category Category,think_user User')
		//  ->field('JmxxcxView.id,JmxxcxView.name,JmxxcxView.title,Category.title as category_name,User.name as username')
		//  ->order('JmxxcxView.id desc')
		//  ->where('JmxxcxView.category_id=Category.id AND JmxxcxView.user_id=User.id')
		//  ->select();

		// var_dump($Model);exit();
		$where = array();
		
		$che_number = $_POST["che_number"];
	    $name = $_POST["name"];
	    $color = $_POST["color"];
	    $type = $_POST["type"];

	    if ($che_number) {
	    	$where['che_number'] = array('like',"%$che_number%");
	    }
	    if ($color) {
	    	$where['color'] = array('like',"%$color%");
	    }
	    if($type){
	    	$where['type'] = array('like',"%$type%");
	    } 
	    if ($name) {
	    	// $sql = "select * from zn_people_che inner join zn_people on zn_people_che.renid = zn_people.id inner join "; 
	    	// $sql['name'] = array('like',"%$name%");
	    	$where['name'] = array('like',"%$name%");
	    }

	    $zn_checount = $Model->where($where)->count(); 
	    $zn_che = $Model->where($where)->select();

	    // $zn_checount = $zn_cheModel->where($where)->count();  
	    // $zn_che = $zn_cheModel->where($where)->select();


	    // var_dump($zn_cheModel->getLastSql());exit;
	    // print_r($zn_che);
	    // die();


		// $zn_peoplecount = $zn_peopleModel->where($where)->count();
	    // $zn_people = $zn_peopleModel->join(array('__ZN_PEOPLE_CHE__ ON __ZN_PEOPLE_CHE__.renid = __ZN_PEOPLE__.id','__ZN_CHE__ on __ZN_CHE__.cheid = __ZN_PEOPLE_CHE__.cheid'))->where($sql)->select();

	 	// $zn_people = $zn_peopleModel->where($where)->select();
	 	// $zn_people = $zn_peopleModel->join('__ZN_PEOPLE_CHE__ ON __ZN_PEOPLE_CHE__.renid = __ZN_PEOPLE__.id')->join('__ZN_CHE__ on __ZN_CHE__.cheid = __ZN_PEOPLE_CHE__.cheid')->where($where)->select();

	    if ($zn_checount > 0){   
	    	echo json_encode($zn_che);

	        // $this->assign('zn_che',$zn_che);
	        // $this->display();         
	    }
	    // if ($zn_peoplecount > 0){          
	    //     $this->assign('zn_che',$zn_people);
	    //     $this->display();        
	    // }
	    else{
	    	echo json_encode(0);
	        // $this->error("检索失败或无选定字段名的数据！",U("admin/Jmxxcx/xx_query"));
	    }
	}
	//车信息预览框
	public function che_xxylk(){
		$glModel = D("JmxxcxView");
		
		$cheid = $_POST["cheid"];

		$where = array();
 		$where['cheid'] = array('eq',"$cheid");
 		$zn_che = $glModel->where($where)->find();

	    // $zn_che = $glModel->find($cheid);    此方法会出错	    
	    echo json_encode($zn_che);        
	}
	//通过单位id查询信息
	public function danweiDetail($id){  //传入的是单位id
	    $this->display('danwei');
	}
	//通过车id获取详细的信息
	public function cheDetail($id){
	    $this->display('che');
	}
}